import {convertTimestampToObj, DateFormatter} from "./time-format.js";

export function formatSize(value) {
    const kKb = 1024;
    const kMb = kKb * 1024;
    const kGb = kMb * 1024;
    if (value > kGb) {
        return (value / kGb).toFixed(2) + 'GB';
    } else if (value > kMb) {
        return (value / kMb).toFixed(2) + 'MB';
    } else if (value > kKb) {
        return (value / kKb).toFixed(2) + 'KB';
    } else {
        return value + 'B';
    }
}
export class ChartDateFormatter {
    constructor(formatString) {
        this.formatter = new DateFormatter(formatString);
    }
    format(timestamp) {
        return this.formatter.format(timestamp);
    }
}
ChartDateFormatter.createDefault = function() {
    return new ChartDateFormatter("year-mon-day-hour:min:sec.ms.us");
}
ChartDateFormatter.createByRange = function(beginTimestamp, endTimestamp) {
    let begin = convertTimestampToObj(beginTimestamp);
    let end = convertTimestampToObj(endTimestamp);
    let formatStrings = [
        ["year", "year-mon-day-hour:min:sec.ms.us"],
        ["month", "mon-day-hour:min:sec.ms.us"],
        ["day", "day-hour:min:sec.ms.us"],
        ["hour", "hour:min:sec.ms.us"],
        ["minute", "hour:min:sec.ms.us"],
        ["second", "hour:min:sec.ms.us"],
        ["ms", "hour:min:sec.ms.us"],
        ["us", "hour:min:sec.ms.us"],
    ];
    let formatString = formatStrings[0][1];
    for (let keyAndFormat of formatStrings) {
        let key = keyAndFormat[0];
        if (begin[key] !== end[key]) {
            formatString = keyAndFormat[1];
            break;
        }
    }
    return new ChartDateFormatter(formatString);
}
